/*
 * Copyright (C) 2012, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.environment;

import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Laurent WOUTERS
 */
public class Hooks {
    private static final String hookNmspce = "http://www.xowl.org/library/";
    
    private xowl.gmi.environment.MainController main;
    private xowl.interpreter.Repository repository;
    private Collection<xowl.interpreter.Hook> hooks;
    
    public Collection<xowl.interpreter.Hook> get() { return hooks; }
    
    public Hooks(xowl.gmi.environment.MainController main, xowl.interpreter.Repository repository) {
        this.main = main;
        this.repository = repository;
        this.hooks = new ArrayList<>();
        
        this.hooks.add(new xowl.interpreter.Hook(repository.resolveEntity(hookNmspce + "system#print"), 1, false, new xowl.interpreter.Hook.Call() {
            @Override public Object execute(xowl.interpreter.Thread thread, Object[] parameters) {
                Hooks.this.main.getExecution(thread).print(parameters[0].toString());
                return null;
            }
        }));
        
        this.hooks.add(new xowl.interpreter.Hook(repository.resolveEntity(hookNmspce + "system#createEntity"), 1, true, new xowl.interpreter.Hook.Call() {
            @Override public Object execute(xowl.interpreter.Thread thread, Object[] parameters) {
                String onto = (String)parameters[0];
                String iri = onto + "#" + xowl.gmi.model.DiagramNode.generateID("get");
                return Hooks.this.main.getRepository().resolveEntity(iri);
            }
        }));
        
        this.hooks.add(new xowl.interpreter.Hook(repository.resolveEntity(hookNmspce + "system#getOntology"), 0, true, new xowl.interpreter.Hook.Call() {
            @Override public Object execute(xowl.interpreter.Thread thread, Object[] parameters) {
                ProjectExecution pe = Hooks.this.main.getExecution(thread);
                return pe.getOntology();
            }
        }));
        
        this.hooks.add(new xowl.interpreter.Hook(repository.resolveEntity(hookNmspce + "system#createDiagram"), 3, false, new xowl.interpreter.Hook.Call() {
            @Override public Object execute(xowl.interpreter.Thread thread, Object[] parameters) {
                xowl.lang.runtime.Entity model = (xowl.lang.runtime.Entity)parameters[0];
                String viewID = (String)parameters[1];
                String name = (String)parameters[2];
                Hooks.this.main.getExecution(thread).createDiagram(model, viewID, name);
                return null;
            }
        }));
    }
}
